// pages/myself/integral/Integral.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {},
    integiftList: [],
    integral: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    if (app.globalData.userAndTenantInfo) {
      this.setData({
        integral: app.globalData.userAndTenantInfo.integral
      })
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        this.setData({
          integral: res.integral
        })
      }
    }

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();

    this.setData({
      integiftList:[]
    })
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId}, integralsuccess);
    network._post_page_json(this, api.integiftList, start, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  integralRule:function(){
    wx.navigateTo({
      url: '/pages/myself/integral/IntegralRules',
    })
  },
  userDetails: function () {
    wx.navigateTo({
      url: '/pages/myself/integral/IntegralDetails'
    })
  },
  ongroupItemClick:function(e){
    var ipsId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/myself/integral/RedeemDetails?ipsId=' + ipsId
    })
  }
}))
var integralsuccess = function(that,res){
  if (res.data.data) {
    that.setData({
      integral: res.data.data.integral
    })
  }
}

var success = function(that,res){
  if(res.data.data){
    that.setData({
      integiftList: res.data.data
    })
  }
}